package com.cskaoyan.user.service.impl;

import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserVerifyRequest;
import com.cskaoyan.user.dto.UserVerifyResponse;
import com.cskaoyan.user.service.IRegisterVerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
@Service
public class RegisterVerifyImpl implements IRegisterVerifyService {
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    MemberMapper memberMapper;
    @Override
    @Transactional
    public UserVerifyResponse verify(UserVerifyRequest userVerifyRequest) {
        //
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();

        //参数检查
        userVerifyRequest.requestCheck();
        //根据uuid去修改用户校验表
        Example verifyExample = new Example(UserVerify.class);
        verifyExample.createCriteria().andEqualTo("uuid",userVerifyRequest.getUuid());
        UserVerify userVerify = new UserVerify();
        userVerify.setIsVerify("Y");
        int i1 = userVerifyMapper.updateByExampleSelective(userVerify, verifyExample);
        if (i1!=1){
            userVerifyResponse.setCode(UserRetCode.USER_REGISTER_VERIFY_FAILED.getCode());
            userVerifyResponse.setMsg(UserRetCode.USER_REGISTER_VERIFY_FAILED.getMessage());
            return userVerifyResponse;
        }
        //根据用户名去修改用户成员表
        Example memberExample = new Example(Member.class);
        memberExample.createCriteria().andEqualTo("username",userVerifyRequest.getUserName());
        Member member = new Member();
        member.setIsVerified("Y");
        int i = memberMapper.updateByExampleSelective(member,memberExample);
        if (i!=1){
            userVerifyResponse.setCode(UserRetCode.USER_REGISTER_VERIFY_FAILED.getCode());
            userVerifyResponse.setMsg(UserRetCode.USER_REGISTER_VERIFY_FAILED.getMessage());
            return userVerifyResponse;
        }
        userVerifyResponse.setCode(UserRetCode.SUCCESS.getCode());
        userVerifyResponse.setMsg(UserRetCode.SUCCESS.getMessage());
        return userVerifyResponse;
    }
}
